package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.AppMedicineorder;
import com.sqx.modules.health.service.IAppMedicineorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hsx
 * @since 2022-09-16
 */
@Slf4j
@Api(value = "", tags = "", description = "")
@RestController
@RequestMapping("/health/app/medicineorder")
public class AppMedicineorderController {
    @Autowired
    private IAppMedicineorderService appMedicineorderService;

    // 仅新增
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新", notes = "新增或更新")
    public Result save(@RequestBody AppMedicineorder appMedicineorder){
        return Result.success(appMedicineorderService.save(appMedicineorder));
    }

    // 新增或更新通用
    @PostMapping("/save2")
    @ApiOperation(value = "新增或更新", notes = "新增或更新")
    public Result save2(@RequestBody AppMedicineorder appMedicineorder){
        return Result.success(appMedicineorderService.saveOrUpdate(appMedicineorder));
    }

    // 根据ID物理删除
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    public Result delete(@PathVariable Integer id){
        return Result.success(appMedicineorderService.removeById(id));
    }
    // 逻辑删除不显示
    @PostMapping("/del2")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    public Result delete2(@RequestBody AppMedicineorder appMedicineorder){
    appMedicineorder.setIsdelete(1);
        return Result.success(appMedicineorderService.saveOrUpdate(appMedicineorder));
        }
    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除", notes = "根据IDS删除")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(appMedicineorderService.removeByIds(ids));
    }

    // 批量删除逻辑删除
    @PostMapping("/del/batch2")
    @ApiOperation(value = "根据IDS删除", notes = "根据IDS删除")
    public Result deleteBatch2(@RequestBody List<Integer> ids){
        log.info("deleteBatch2 method start............");
        LambdaUpdateWrapper<AppMedicineorder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AppMedicineorder::getIsdelete , 1);
        wrapper.in(ObjectUtil.isNotEmpty(ids),AppMedicineorder::getId, ids);
        boolean deleteBatch2 = appMedicineorderService.update(wrapper);
        log.info("deleteBatch2 method is {}", deleteBatch2);
        return Result.success(deleteBatch2);
        }

    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    public Result findAll(){
        return Result.success(appMedicineorderService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录", notes = "根据ID查询一条记录")
    public Result findOne(@PathVariable Integer id){
        return Result.success(appMedicineorderService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String searchName){
        QueryWrapper<AppMedicineorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdelete", 0);
        queryWrapper.like(ObjectUtil.isNotEmpty(searchName),"username", searchName);
        queryWrapper.or().like(ObjectUtil.isNotEmpty(searchName),"doctorname", searchName);
        queryWrapper.orderByDesc("id");
        return  Result.success(appMedicineorderService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }

    // 分页查询
    @PostMapping("/findConditionPage")
    @ApiOperation(value = "根据条件分页查询", notes = "根据条件分页查询")
    public Result findConditionPage(@RequestParam Map<String, String> params){
        params.put("pageNum","1");
        params.put("pageSize","10");
            QueryWrapper<AppMedicineorder> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(ObjectUtil.isNotEmpty(params.get("searchName")),"name", params.get("searchName"));
            queryWrapper.eq("isdelete", 0);
            queryWrapper.orderByDesc("id");
        return  Result.success(appMedicineorderService.page(new Page<>(Integer.parseInt(params.get("pageNum")),Integer.parseInt(params.get("pageSize"))),queryWrapper));
    }

    // 条件查询不分页
    @PostMapping("/findCondition")
    @ApiOperation(value = "根据条件查询多条", notes = "根据条件查询多条")
    public Result findCondition(@RequestParam Map<String, String> params){
        QueryWrapper<AppMedicineorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(params.get("isdelete")), "isdelete", 0);
        //根据需要写入条件 默认条件查询框的业务实体名称
        queryWrapper.like(ObjectUtil.isNotEmpty(params.get("searchName")),"name", params.get("searchName"));
        queryWrapper.orderByDesc("id");
        return  Result.success((List<AppMedicineorder>)appMedicineorderService.list(queryWrapper));
        }
}

